using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Circa;

namespace Circa.Protocol.Irc {

    public class NewServerConnectionEventArgs: EventArgs {

        public Server Server { get; private set; }

        public NewServerConnectionEventArgs( Server server ) {
            Server = server;
        }

    }

    public class Network {
        // Configuration properties
        public string Name { get; private set; }
        public string Description { get; private set; }
        public bool AutoConnect { get; private set; }
        public List<SelfUser> Identities { get; private set; }
        public List<Server> Servers { get; set; }
        public List<Channel> Channels { get; set; }

        // Runtime properties
        public SelfUser Self { get; set; }

        // Public events
        public event EventHandler<NewServerConnectionEventArgs> NewServerConnection;
        private void OnNewServerConnection( Server server ) {
            var evt = NewServerConnection;
            if ( null != evt ) {
                evt( this, new NewServerConnectionEventArgs( server ) );
            }
        }

        // Constructors
        protected Network( ) {
            Identities = new List<SelfUser>( );
            Servers = new List<Server>( );
            Channels = new List<Channel>( );
        }

        public Network( Configuration.Network confNetwork ): this( ) {
            foreach ( Configuration.Identity conf in confNetwork.Identities ) {
                Identities.Add( new SelfUser( this, conf ) );
            }
            foreach ( Configuration.Server conf in confNetwork.Servers ) {
                Servers.Add( new Server( this, conf ) );
            }
            foreach ( Configuration.Channel conf in confNetwork.Channels ) {
                Channels.Add( new Channel( this, conf ) );
            }
            Name = confNetwork.Name;
            Description = confNetwork.Description;
            AutoConnect = confNetwork.AutoConnect;
        }

        // Public methods
        public void Connect( ) {
            Server s = Servers[ 0 ];
            s.Self = Identities[ 0 ];
            OnNewServerConnection( s );
            s.Connect( );
        }

    }

}
